package tables

import (
	"peilian-api/app/global/variable"
)

type GetOptResumeListResp struct {
	*ResumeRecord
	BeijingTime
}
type BeijingTime struct {
	UpdTimeStr    string `json:"upd_time_str"`
	CreateTimeStr string `json:"created_time_str"`
}

type ResumeRecord struct {
	Model
	User_id      uint   `json:"user_id" gorm:"default:0;comment:创建者id"`
	Name         string `json:"name" validate:"required" gorm:"type:varchar(40);not null;comment:优化简历名称"`
	Jd           string `json:"jd" gorm:"type:varchar(2048);comment:职位描述"`
	Exp          string `json:"exp" validate:"required" gorm:"type:varchar(2048);comment:经历实践"`
	Opt_exp      string `json:"opt_exp" gorm:"type:longtext;comment:优化后经历实践"`
	ResumeStatus uint   `gorm:"type:tinyint(1);default:0;comment:简历生成状态,0-未生成，1-生成中，2-已生成，3-生成失败" json:"resume_status"`
	IsViewed     uint   `gorm:"type:tinyint(1);default:0;comment:是否已查看，0-未查看，1-已查看" json:"is_viewed"`
}

func (r *ResumeRecord) UpdateResumeById(updates map[string]interface{}, id int) error {
	if d := variable.DB.MySQL.Model(r).Where("id = ?", id).Updates(updates).Limit(1); d.Error != nil {
		return d.Error
	}
	return nil
}

// 根据id删除某条简历
func (r *ResumeRecord) DeleteResumeById(id int) error {
	if err := variable.DB.MySQL.Delete(r, "id=?", id).Limit(1).Error; err != nil {
		return err
	}
	return nil
}

// 根据条件查询简历
func (r *ResumeRecord) GetResumesByWhere(whereMap map[string]interface{}, whereStr string) ([]ResumeRecord, interface{}) {
	var resumes []ResumeRecord
	if len(whereMap) == 0 {
		if d := variable.DB.MySQL.Model(ResumeRecord{}).Where(whereStr).Find(&resumes); d.Error != nil {
			return resumes, d.Error
		}
	} else if len(whereStr) == 0 {
		if d := variable.DB.MySQL.Model(ResumeRecord{}).Where(whereMap).Find(&resumes); d.Error != nil {
			return resumes, d.Error
		}
	} else {
		if d := variable.DB.MySQL.Model(ResumeRecord{}).Where(whereMap).Where(whereStr).Find(&resumes); d.Error != nil {
			return resumes, d.Error
		}
	}

	return resumes, nil
}
